<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>ajax框架dwr的用法</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">  
	<script type="text/javascript"  src="../js/jquery-1.10.2.js"></script>
	<!--其中jsCallJava.js是dwr根据配置文件自动生成的，engine.js和util.js是dwr自带的脚本文件。 -->  
	<script type="text/javascript" src="dwr/engine.js"></script>  
    <script type="text/javascript" src="dwr/util.js"></script>  
    <script type="text/javascript" src="dwr/interface/jsCallJava.js"></script>  
	<script type="text/javascript">  
		//注意js调用java方法，要传递一个回调函数
		function test1(){  
		    jsCallJava.getCurrentTime(function(data){
		    	//这里可以进行参数处理DWRUtil 的 setValue() 方法会将传回的 msg 设定给指定 id 的 DOM  
		    	 DWRUtil.setValue('currentTime',data); 
		    	alert(data);
		    });  
		}
		
		function test2(){  
		    jsCallJava.say('张三',
		        function(data){  
		            alert(data);  
		        }  
		    );  
		} 
		
		function test3(){  
		    jsCallJava.getUserList(
		        function(data){  
		            var msg="";
		            for(var i=0;i<data.length;i++){
		            	var id=data[i].id;
		            	var name=data[i].name;
		            	var sex=data[i].sex;
		            	var address=data[i].address;
		            	msg+="id:"+id+",name:"+name+",sex:"+sex+",address:"+address+"<br/>";
		            }  
		            $("body").append(msg);
		        }  
		    );  
		}  
		function test4(){  
		    jsCallJava.getUser(
		        function(data){  
		            var msg="";
	            	var id=data.id;
	            	var name=data.name;
	            	var sex=data.sex;
	            	var address=data.address;
	            	msg+="id:"+id+",name:"+name+",sex:"+sex+",address:"+address+"<br/>";  
		            $("body").append(msg);
		        }  
		    );  
		}  
		 
</script>

  </head>
  
  <body>
   当前时间：<input type="text" id="currentTime"/><br/>
   <input type="button" value="测试dwr的无参数，有返回值的方法" onclick="test1()"/><br/>
   <input type="button" value="测试dwr的有参数，有返回值的方法" onclick="test2()"/><br/>
   <input type="button" value="测试dwr的返回list" onclick="test3()"/><br/>
   <input type="button" value="测试dwr的返回对象" onclick="test4()"/><br/>
  </body>
</html>
